
# 正式生产环境
import webbrowser
import json
import paramiko
import os
import subprocess
from script import *


remote_server='standprod_remote_server'

with open('./base_const_file.json', 'r', encoding='utf-8') as base_json_file:
    base_const = json.load(base_json_file)
    
with open('./const.json', 'r', encoding='utf-8') as json_file:
    data = json.load(json_file)

# 读取配置数据
remote_server = data[remote_server]
work_dir = '/Users/code/ct-enterprise-code/rcgy/p-h-broadband-web/ruoyi-vue-web'
local_jar_path = base_const['local_web_path'] + '/' + remote_server['dist_name']
remote_jar_path = remote_server['remote_jar_path']
os.chdir(work_dir)
process = subprocess.run(remote_server['command'],shell=True, stdout=subprocess.PIPE,  stderr=subprocess.PIPE,  universal_newlines=True)
if remote_server['pkey'] != 'None':
    private_key = paramiko.RSAKey.from_private_key_file(remote_server['pkey'])
else:
    private_key = None
               
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(remote_server['host'], remote_server['port'], remote_server['username'], password=remote_server['password'],pkey=private_key)
sftp = ssh.open_sftp()
               
# 上传
upload_dir(sftp, local_jar_path, remote_jar_path)
               
# 释放
sftp.close()
ssh.close()
               
#打开地址
webbrowser.open_new(remote_server['web_url'])

print('deploy complete!')
    